package chinaums.cfps.send;


import chinaums.common.util.CfpsSendMQAppModel;
import chinaums.service.impl.CfpsMQFactorySendTopicImpl;

import com.chinaums.ibmmq.MQMsg;

public class MQSendUtila {
	private static CfpsSendMQAppModel sendMQAppModelForTopic;

	/**
	 * 初始化.
	 */
	public static boolean init() {
		try {
			if (null != sendMQAppModelForTopic) {
				sendMQAppModelForTopic.destory();
			}
			sendMQAppModelForTopic = new CfpsSendMQAppModel();
			String host = "172.30.252.10";
			int port = 1416;
			String queueManager = "QMTEST";
			String channel = "TESTC";
			String topic = "Test";
			String projectName = "test";
			String moduleName = "test";
			sendMQAppModelForTopic.setFactory(new CfpsMQFactorySendTopicImpl(
					host, port, queueManager, channel, topic, projectName,
					moduleName));
			sendMQAppModelForTopic.init();
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	public static MQMsg sendForTopic(String data) {
		MQMsg res = null;
		int count = 3;
		int re = 0;
		while(true){
			if(-1 != count && re >= count){
				break;
			}
			res = sendMQAppModelForTopic.execute(data);
			if (null != res) {
				break;
			}
			try {
				Thread.sleep(10000);
				init();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		return res;
	}

	/**t8     [6217232323457575, 6217232323357575, 6217232323257575, 6217232323157575, 6217232323057575]
	 * 销毁.
	 */
	public static void destory() {
		if (null != sendMQAppModelForTopic) {
			sendMQAppModelForTopic.destory();
		}
	}
	
	public static void main(String[] args) {
		System.out.println("放到数据库");
		init();
		//MQMsg mqMsg = sendForTopic("{\"settle_dt\":\"20150517\",\"auth_id_resp_cd\":\"N\",\"pos_cond_cd\":\"63\",\"card_accptr_nm_loc\":\"F43zhangjiangsoftwarepark\",\"msg_type\":\"0001\",\"term_id\":\"F4112345\",\"card_seq_id\":\"\",\"orig_sys_tra_no\":\"002903\",\"acq_resp_cd\":\"00\",\"trans_curr_cd\":\"156\",\"orig_transmsn_dt_tm\":\"0812173615\",\"msg_tp\":\"0200\",\"rcv_ins_id_cd\":\"00250001\",\"acq_ins_id_cd\":\"48061111\",\"trans_id\":\"S31\",\"mchnt_cd\":\"F42012345678912\",\"trans_at\":\"000000009900\",\"mchnt_tp\":\"7011\",\"card_attr\":\"02\",\"transmsn_dt_tm\":\"0812173620\",\"tfr_in_acct_no\":\"\",\"trans_tp_id\":\"0\",\"card_media\":\"1\",\"pri_acct_no\":\"6225250101111116\",\"retri_ref_no\":\"000000002903\",\"msg_version\":\"0000\",\"fwd_ins_id_cd\":\"00040000\",\"rsn_cd\":\"0000\",\"iss_ins_id_cd\":\"00550001\",\"pos_entry_md_cd\":\"021\",\"cups_def_fld\":\"0000C000030000000000001\",\"sys_tra_no\":\"009901\",\"proc_cd\":\"000000\"}");
													//银行卡号，消费时间，消费地点，消费金额  
		  
//		  MQMsg mqMsg1 = sendForTopic("{\"settle_bacrd\":\"6217232323257666\",\"pos_cond_money\":\"300\",\"pos_cond_time\":\"12:02\",\"pos_cond_home\":\"上海\"}");
//		  MQMsg mqMsg2 = sendForTopic("{\"settle_bacrd\":\"6217232323457575\",\"pos_cond_money\":\"200\",\"pos_cond_time\":\"12:02\",\"pos_cond_home\":\"上海\"}");
//		  MQMsg mqMsg3 = sendForTopic("{\"settle_bacrd\":\"6217232323257690\",\"pos_cond_money\":\"400\",\"pos_cond_time\":\"12:02\",\"pos_cond_home\":\"上海\"}");
//		  MQMsg mqMsg4 = sendForTopic("{\"settle_bacrd\":\"6217232323357575\",\"pos_cond_money\":\"800\",\"pos_cond_time\":\"12:02\",\"pos_cond_home\":\"上海\"}");
//		  MQMsg mqMsg5 = sendForTopic("{\"settle_bacrd\":\"6217232323257575\",\"pos_cond_money\":\"600\",\"pos_cond_time\":\"12:02\",\"pos_cond_home\":\"上海\"}");
		//MQMsg mqMsg = sendForTopic("guaguagua");
		//System.out.println(mqMsg.getMsg());
		for(int i=0;i<1000;i++){ 
			MQMsg mqMsg1 = sendForTopic(
				  "{\"pri_acct_no\":\"6217232323257666\",\"trans_at\":\"300\",\"pos_cond_time\":\"12:02\",\"pos_cond_home\":\"上海\"}"
				  ); 
			}
	}

}
